/**
 *
 */
package com.iknight.mgr.core.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.util.Base64Utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Jeryzym
 */
public final class IkgStringUtils {
    private static final Logger logger = LogManager.getLogger(IkgStringUtils.class);

    /**
     * @param min 最小长度
     * @param max 最大长度
     * @return
     * @description 随机生成字符串，
     */
    public static String randomString(int min, int max) {
        String uuid = UUID.randomUUID().toString();
        int len = (new Random()).nextInt(max) % (max - min + 1) + min;
        String salt = uuid.replaceAll("-", "").substring(0, len);
        return salt;
    }

    public static String md5Pwd(String orginalPwd, String salt) {
        MessageDigest md5;
        String result = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            StringBuffer sb = new StringBuffer();
            sb.append(orginalPwd);
            sb.append(salt);
            byte[] md5Bytes = md5.digest(sb.toString().getBytes("UTF-8"));
            StringBuffer hexValue = new StringBuffer();
            for (int i = 0; i < md5Bytes.length; i++) {
                int val = ((int) md5Bytes[i]) & 0xff;
                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }
            result = Base64Utils.encodeToString(hexValue.toString().getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }

    /**
     * @param pattern 日期格式
     * @return
     * @description 获取当前时间
     */
    public static String getCurrentDate(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Calendar now = Calendar.getInstance();
        return sdf.format(now.getTime());
    }

    public static boolean isNull(Object o) {
        return null == o;
    }

    public static boolean isNotNull(Object o) {
        return null != o;
    }

    public static String ObjectToString(Object o) {
        if (null == o) {
            return "";
        }
        return o.toString().trim();
    }

    public static int ObjectToInt(Object o) {
//        logger.error(o);
        if (null == o) {
            return 0;
        }
        if (o.toString().length() == 0) {
            return 0;
        }
        return Integer.parseInt(o.toString().trim());
    }


    public static List removeDuplicate(List list) {
        HashSet hs = new HashSet(list);
        list.clear();
        list.addAll(hs);
        return list;
    }

    /**
     * 获取随机36位编码
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    public static boolean findKeyInArray(List<String> arr, String targetValue) {
        for (String s : arr) {
            if (s.equals(targetValue))
                return true;
        }
        return false;
    }
}
